//
//  LoginFirstViewController.m
//  Application
//
//  Created by 薛小刚 on 2017/12/21.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "B_LoginFirstViewController.h"
#import "SetPassworldViewController.h"
#import "RegisitAccountViewController.h"
#import "LoginTwoViewController.h"
#import "NSString+RegexCategory.h"

@interface B_LoginFirstViewController ()
/// 关闭按钮点击事件
- (IBAction)closeButtonClicked:(id)sender;
/// 手机号的背景
@property (weak, nonatomic) IBOutlet UIView *phoneView;
/// 输入框
@property (weak, nonatomic) IBOutlet UITextField *inputPhoneTextfiled;
/// 下一步的点击事件
- (IBAction)nextStepButtonClicked:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *nextStepButton;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@end

@implementation B_LoginFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isAnimation = NO;
    [self setUI];
    [self getImage];
    [self.view sendSubviewToBack:self.backgroundImageView];
}
- (void)getImage {
    [NetworkingManager getNetworkRequestWithUrlString:@"http://39.106.113.252/bgmimg.json" parameters:nil isCache:NO succeed:^(id data) {
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:data[@"img"]]];
    } fail:^(NSError *error) {
        self.backgroundImageView.backgroundColor = [UIColor blackColor];
    }];
}

static bool isAnimation;
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:isAnimation];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}


/**
 设置UI
 */
- (void) setUI {
    self.phoneView.layer.masksToBounds      = YES;
    self.phoneView.layer.cornerRadius       = 4;
    self.phoneView.layer.borderColor        = xgLineColor.CGColor;
    self.phoneView.layer.borderWidth        = 1;
    self.view.backgroundColor = [UIColor blackColor];
    self.inputPhoneTextfiled.placeholder = @"请输入手机号";
    self.inputPhoneTextfiled.text = getMobile();
    [self.inputPhoneTextfiled setValue:UIColorFromRGB(0xafafaf) forKeyPath:@"_placeholderLabel.textColor"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 关闭按钮

 @param sender 按钮
 */
- (IBAction)closeButtonClicked:(id)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 下一步按钮

 @param sender 按钮
 */
- (IBAction)nextStepButtonClicked:(id)sender {
    [self.view endEditing:YES];
    if (ISNULL(self.inputPhoneTextfiled.text)) {
        [UIView xg_toast:@"请输入手机号码"];
        return;
    }
    if ([self.inputPhoneTextfiled.text isMobileNumber]) {
        
    } else {
        [UIView xg_toast:@"请输入有效的手机号码"];
        return;
    }
    setMobile(self.inputPhoneTextfiled.text);
    [self getDataFromService];
}

- (void) getDataFromService {
    NSString * stringPath = [NSString stringWithFormat:@"/yearnfor/inter/login/phoneIsRegister/%@", self.inputPhoneTextfiled.text];
    [NetworkingManager getNetworkRequestWithUrlString:stringPath parameters:nil isCache:NO succeed:^(id data) {
        NSLog(@"data is %@", data);
        if ([data[@"code"] isEqualToString:@"2"]) { // 未注册
            RegisitAccountViewController * regisit = [[RegisitAccountViewController alloc] init];
            [self pushNextViewController:regisit];
        } else if ([data[@"code"] isEqualToString:@"1"]) {                                    // 已注册
            LoginTwoViewController * login = [[LoginTwoViewController alloc] init];
            [self pushNextViewController:login];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
//    [NetworkManger getNetworkRequestWithUrlString:stringPath parameters:nil isCache:NO succeed:^(id data) {
//        if ([data[@"code"] intValue] == 20010) {
//            [[NSUserDefaults standardUserDefaults] setObject:self.inputPhoneTextfiled.text forKey:PHONE_NUMBER];
//            if ([data[@"values"][@"isRegister"] intValue] == 1) {       /// 已注册
//                if ([data[@"values"][@"isPassword"] intValue] == 1) {   /// 有密码
//                    LoginTwoViewController * login = [[LoginTwoViewController alloc] init];
//                    [self pushNextViewController:login];
//                } else if ([data[@"values"][@"isPassword"] intValue] == 2) {   /// 没有密码
//                    SetPassworldViewController * pass = [[SetPassworldViewController alloc] init];
//                    [self pushNextViewController:pass];
//                }
//            } else if ([data[@"values"][@"isRegister"] intValue] == 2) {    /// 未注册
//                RegisitAccountViewController * regisit = [[RegisitAccountViewController alloc] init];
//                [self pushNextViewController:regisit];
//            }
//        } else {
//            [KeyWindow makeToast:data[@"message"]];
//        }
//    } fail:^(NSError *error) {
//        HXLog(@"获取手机号的状态失败了");
//    }];
    
}


- (BOOL)shouldAutorotate {
    [super shouldAutorotate];
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    return UIInterfaceOrientationMaskPortrait;
    
}


- (void)dealloc {
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
